package edu.hwadee.action;

import java.util.List;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ModelDriven;

import edu.hwadee.entity.Material;
import edu.hwadee.entity.Materialstorage;
import edu.hwadee.entity.Storage;

public class MaterialstorageAction extends BasicAction implements ModelDriven<Materialstorage> {

	private Materialstorage materialstorage;
	private int msmaterialid;
	private int msstorageid;
	
	@Override
	public Materialstorage getModel() {
		if(getMaterialstorage() == null) 
			setMaterialstorage(new Materialstorage());
		return getMaterialstorage();
	}

	public String showAll() {
		System.out.println("*********");
		List<Materialstorage> list = getMaterialstorageServiceImpl().showAll();
		ServletActionContext.getRequest().getSession().setAttribute("materialstoragelist", list);
		return "success";
	}
	
	public String deleteMaterialStorage() {
		getMaterialstorageServiceImpl().delete(materialstorage);
		return "success";
	}
	
	public String updateMaterialStorage() {
		Material material =getMaterialServiceImpl().searchById(msmaterialid);
		Storage storage = getStorageServiceImpl().searchById(msstorageid);
		materialstorage.setMaterial(material);
		materialstorage.setStorage(storage);
		getMaterialstorageServiceImpl().update(materialstorage);
		return "success";
	}
	
	public String addMaterialStorage() {
		System.out.println(msmaterialid + "**" + msstorageid);
		Material material =getMaterialServiceImpl().searchById(msmaterialid);
		Storage storage = getStorageServiceImpl().searchById(msstorageid);
		System.out.println(material + "--" + storage);
		materialstorage.setMaterial(material);
		materialstorage.setStorage(storage);
		getMaterialstorageServiceImpl().save(materialstorage);
		return "success";
	}
	
	public String searchMaterialStorage() {
		Materialstorage ms = getMaterialstorageServiceImpl().searchById(materialstorage.getMaterialstorageId());
		ServletActionContext.getRequest().getSession().setAttribute("materialstoragelist", ms);
		ServletActionContext.getRequest().getSession().setAttribute("searchMStorage", true);
		setMaterialstorage(null);
		return "success";
	}
	
	public String searchStorage() {
		return "success";
	}
	
	public Materialstorage getMaterialstorage() {
		return materialstorage;
	}

	public void setMaterialstorage(Materialstorage materialstorage) {
		this.materialstorage = materialstorage;
	}

	public int getMsmaterialid() {
		return msmaterialid;
	}

	public void setMsmaterialid(int msmaterialid) {
		this.msmaterialid = msmaterialid;
	}

	public int getMsstorageid() {
		return msstorageid;
	}

	public void setMsstorageid(int msstorageid) {
		this.msstorageid = msstorageid;
	}
	
}
